package dimension.starry.gui;

import dimension.starry.constants.*;
import dimension.starry.item.typeInterface.*;
import dimension.starry.item.typeInterface.WeaponType.WeaponType;
import dimension.starry.utils.StringUtil;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.CropBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.item.*;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 150149
 */

@Environment(EnvType.CLIENT)
public class ShowItemDescription {

    private static final Text SPLIT = Text.translatable("starry_dimension.gui.desc.split");

    public static void onItemTooltip(ItemStack itemStack, TooltipContext context, List<Text> lines) {
        List<Text> oldToolTip = new ArrayList<>(lines);
        Text name = oldToolTip.get(0);
        oldToolTip.remove(0);
        lines.clear();

        appendName(lines, itemStack, name);
        lines.add(SPLIT);
        appendType(lines, itemStack);
        appendFood(lines, itemStack);
        appendWeaponDesc(lines, itemStack);
        appendArmorDesc(lines, itemStack);

        if (!oldToolTip.isEmpty()) {
            lines.add(SPLIT);
            lines.addAll(oldToolTip);
        }
        lines.add(SPLIT);
    }

    private static void appendType(List<Text> lines, ItemStack itemStack) {
        if (itemStack.getItem().getFoodComponent() != null || itemStack.getItem() instanceof FoodType) {
            lines.add(Text.translatable("starry_dimension.gui.dsc3"));
        } else if (itemStack.getItem() instanceof BlockItem &&
            ((BlockItem) itemStack.getItem()).getBlock() instanceof CropBlock) {
            lines.add(Text.translatable("starry_dimension.gui.dsc13"));
        } else if (itemStack.getItem() instanceof BlockItem) {
            lines.add(Text.translatable("starry_dimension.gui.dsc6"));
        } else if (itemStack.getItem() instanceof DyeItem) {
            lines.add(Text.translatable("starry_dimension.gui.dsc7"));
        } else if (itemStack.getItem() instanceof EnchantedBookItem) {
            lines.add(Text.translatable("starry_dimension.gui.dsc8"));
        } else if (itemStack.getItem() instanceof PotionItem) {
            lines.add(Text.translatable("starry_dimension.gui.dsc9"));
        } else if (itemStack.getItem() instanceof WeaponType) {
            lines.add(Text.translatable("starry_dimension.gui.dsc11"));
        } else if (itemStack.getItem() instanceof SwordItem) {
            lines.add(Text.translatable("starry_dimension.gui.dsc11"));
        } else if (itemStack.getItem() instanceof ToolItem) {
            lines.add(Text.translatable("starry_dimension.gui.dsc14"));
        } else if (itemStack.getItem() instanceof MaterialType) {
            lines.add(Text.translatable("starry_dimension.gui.dsc15"));
        } else if (itemStack.getItem() instanceof ComponentType) {
            lines.add(Text.translatable("starry_dimension.gui.dsc19"));
        } else if (itemStack.getItem() instanceof ArmorItem) {
            lines.add(Text.translatable("starry_dimension.gui.dsc5"));
        } else {
            lines.add(Text.translatable("starry_dimension.gui.dsc16"));
        }

        if (itemStack.getItem() instanceof ALLItemType allItemType) {
            lines.add(allItemType.getQuality().getTranslationTag());
        } else if (itemStack.getItem() instanceof BlockItem blockItem && blockItem.getBlock() instanceof ALLItemType allItemType) {
            lines.add(allItemType.getQuality().getTranslationTag());
        } else {
            lines.add(ItemQuality.NORMAL.getTranslationTag());
        }
    }

    private static void appendName(List<Text> lines, ItemStack itemStack, Text name) {
        lines.add(Text.of(name.getString() + " [" + itemStack.getDamage() + "/" + itemStack.getMaxDamage() + "]"));
    }

    private static void appendFood(List<Text> lines, ItemStack itemStack) {
        NbtCompound nbtCompound = itemStack.getNbt();
        ClientWorld world = MinecraftClient.getInstance().world;
        if (nbtCompound != null && itemStack.getItem().getFoodComponent() != null && world != null) {
            if (nbtCompound.contains(ItemNbtTag.FOOD_CREATED_TIME)) {
                lines.add(SPLIT);
                long foodTime = nbtCompound.getLong(ItemNbtTag.FOOD_CREATED_TIME);
                long i = 100L - (world.getTime() - foodTime) / 1000L;
                lines.add(Text.translatable("starry_dimension.gui.dsc4").append(i + "%"));
            }
        }
    }

    private static void appendWeaponDesc(List<Text> lines, ItemStack itemStack) {
        if (itemStack.getItem() instanceof WeaponType weaponType) {
            lines.add(SPLIT);
            WeaponType.getAttributeMap(itemStack).forEach((weaponAttribute, value) -> {
                if (weaponAttribute.isPercentage()) {
                    lines.add(Text.of(
                        weaponAttribute.getTranslationTag().getString() + StringUtil.formatDouble(value * 100) + "%"));
                } else {
                    lines.add(Text.of(
                        weaponAttribute.getTranslationTag().getString() + StringUtil.formatDouble(value)));
                }
            });
            lines.add(Text.empty());
            lines.add(
                Text.translatable("starry_dimension.weapon_attribute.level")
                    .append(WeaponType.getLevel(itemStack) + "/" + WeaponType.getMaxLevel(itemStack))
            );
            lines.add(
                Text.translatable("starry_dimension.weapon_attribute.exp")
                    .append(WeaponType.getExp(itemStack) + "/" +
                        WeaponExp.getNeedExp(weaponType, WeaponType.getLevel(itemStack)))
            );

            String mergeCount = String.valueOf(WeaponType.getMergeCount(itemStack));
            Text mergeNum = Text.translatable("starry_dimension.weapon_attribute.merge." + mergeCount);
            lines.add(
                Text.translatable("starry_dimension.weapon_attribute.merge").append(mergeNum)
            );
            lines.add(SPLIT);
            lines.addAll(weaponType.getDescription());
        }
    }

    private static void appendArmorDesc(List<Text> lines, ItemStack itemStack) {
        if (itemStack.getItem() instanceof ArmorType armorItem) {
            lines.add(SPLIT);
            WeaponAttribute mainAttribute = ArmorType.getMainAttribute(itemStack);
            if (mainAttribute == null) {
                return;
            } else if (mainAttribute.isPercentage()) {
                lines.add(Text.of(mainAttribute.getTranslationTag().getString() +
                    StringUtil.formatDouble(ArmorType.getMainAttributeValue(itemStack) * 100) + "%"));
            } else {
                lines.add(Text.of(mainAttribute.getTranslationTag().getString() +
                    StringUtil.formatDouble(ArmorType.getMainAttributeValue(itemStack))));
            }
            lines.add(Text.empty());
            ArmorType.getAttributeMap(itemStack).forEach((weaponAttribute, value) -> {
                if (weaponAttribute.isPercentage()) {
                    lines.add(Text.of(
                        weaponAttribute.getTranslationTag().getString() + StringUtil.formatDouble(value * 100) + "%"));
                } else {
                    lines.add(
                        Text.of(weaponAttribute.getTranslationTag().getString() + StringUtil.formatDouble(value)));
                }
            });
            lines.add(Text.empty());
            lines.add(
                Text.translatable("starry_dimension.weapon_attribute.level")
                    .append(ArmorType.getLevel(itemStack) + "/" + armorItem.getMaxLevel())
            );
            lines.add(
                Text.translatable("starry_dimension.weapon_attribute.exp")
                    .append(ArmorType.getExp(itemStack) + "/" +
                        ArmorExp.getNeedExp(armorItem, ArmorType.getLevel(itemStack)))
            );
        }
    }

}
